package com.ipay.wechat.controller;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ipay.redis.JedisExecutor;
import com.ipay.wechat.component.Wechat;
import com.ipay.wechat.pojo.MerchantInfo;
import com.ipay.wechat.service.ApplyService;

@Controller
public class TestController{
	private Logger logger=LoggerFactory.getLogger(getClass());
	@Autowired
	private JedisExecutor manager;
	@Autowired
	private Wechat wechat;
	@Autowired
	private ApplyService applyService;
	
	
    /*@PostConstruct
	private void init() {
		logger.info("AccessTokenUtil。init");
		AccessToken accessToken=new AccessToken();
		accessToken = AccessTokenUtil.freshAccessToken(wechat);
		String access_token=accessToken.getAccess_token();
		WXTicket Ticket=new WXTicket();
		Ticket = AccessTokenUtil.freshTicket(access_token);
		String ticket=Ticket.getTicket();
		manager.remove(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TOKEN, "access_token");
		manager.remove(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TICKET, "ticket");
		manager.setValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TOKEN, "access_token", access_token, 6500);
		manager.setValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TICKET, "ticket", ticket, 6500);
	}*/
	
	@RequestMapping("/test22")
	public String test22(){
		return "redirect:/test11";
	}
	@RequestMapping("/test11")
	public ModelAndView test(){
		String mobile ="123";
		String merchant_no="12345";
		MerchantInfo info3=applyService.querryMerchantInfoByMerchantNo(merchant_no);
		
		logger.info("info3=="+info3.toString());
		
		return new ModelAndView("aa.jsp");
	}
	
	@RequestMapping("/testlogin")
	public ModelAndView testlogin(){
		//logger.info("取出accesstoken==="+manager.getValue("access_token"));
		return new ModelAndView("login.jsp");
	}
	
	@RequestMapping("/testsigninfo")
	public ModelAndView testsigninfo(){ 
		return new ModelAndView("sign_info.jsp");
	}
	 
	@RequestMapping("/testregister")
	public ModelAndView testregister(){
		//logger.info("取出accesstoken==="+manager.getValue("access_token"));
		return new ModelAndView("register.jsp");
	}
	
	@RequestMapping("/testreceipt")
	public ModelAndView testreceipt(){
		//logger.info("取出accesstoken==="+manager.getValue("access_token"));
		return new ModelAndView("receipt_details.jsp");
	}
	
	@RequestMapping("/testuser")
	public ModelAndView testuser(){
		//logger.info("取出accesstoken==="+manager.getValue("access_token"));
		return new ModelAndView("user_center.jsp");
	}
	@RequestMapping("/testinfo")
	public ModelAndView testinfo(){
		return new ModelAndView("shop_info.jsp");
	}
	public static void main(String[] args) {
		String url="http://imgsrc.baidu.com/imgad/pic/item/caef76094b36acaf0accebde76d98d1001e99ce7.jpg";
		
		downloadPicture(url);
	}
	public static void downloadPicture(String urlList){
		URL url=null;
		try {
			url=new URL(urlList);
			DataInputStream data=new DataInputStream(url.openStream());
			String imageName="F:/test.jpg";
			File imgFile = new File(imageName);
			FileOutputStream fout=new FileOutputStream(imgFile);
			ByteArrayOutputStream bout=new ByteArrayOutputStream();
			
			byte[] buffer=new byte[1024];
			int length;
			
			while((length=data.read(buffer))>0){
				bout.write(buffer,0,length);
			}
			fout.write(bout.toByteArray());
			data.close();
			fout.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}